package com.iwhalecloud.bss.kite.cucc.service.order;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.dto.cust.ContextCustDTO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IPrintTemplateService;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosCustomerDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalDcPublicConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.cucc.spec.impl.transfer.GoodsInfoTransfer;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.*;
import edu.emory.mathcs.backport.java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by huanghaosheng on 2021/3/8 19:34
 */
@Service
public class PrintTemplateService implements IPrintTemplateService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    /**
     * 融合产品-配置单打印
     */
    @Override
    public Map<String, Object> dealPrintTemplatePlural(String sceneInstId, String serviceOfferId,
                                                       ContextCustDTO currentCust) {
        Map<String, Object> result = new HashMap<>();

        /** 订单信息start */
        String serialNumber = ""; // 业务号码
        String custName = ""; // 客户名称
        String city = ""; // 所在城市
        // 集团用户信息模块
        List<KiteAttr> groupUserKiteAttrs = querySceneInstService
            .listCommonAttrsByModuleId(sceneInstId, LocalKeyConsts.GROUPUSERINFO_MODULEID).stream().filter(
                kiteAttr -> !KiteStringUtils.isEqual("H", kiteAttr.getShowType()) && !KiteStringUtils
                    .isEqual("1", kiteAttr.getObjId())).collect(Collectors.toList());
        for (KiteAttr kiteAttr : groupUserKiteAttrs) {
            if (KiteStringUtils.equals("business_number", kiteAttr.getFieldName())) {
                serialNumber = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
            if (KiteStringUtils.equals("user_location", kiteAttr.getFieldName())) {
                city = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
        }
        KitePosCustomerDTO custInfo = querySceneInstService.getExtParameterValue(sceneInstId, "customer");
        custName = Objects.nonNull(custInfo) ? custInfo.getName() : currentCust.getCustName();

        String custManagerName = ""; // 客户经理姓名
        String custManagerPhone = ""; // 客户经理联系电话
        String custManagerEmail = ""; // 客户经理联系邮箱
        // 客户经理信息模块
        List<KiteAttr> custManagerKiteAttrs = querySceneInstService
            .listCommonAttrsByModuleId(sceneInstId, LocalKeyConsts.CUSTMANAGERINFO_MODULEID).stream().filter(
                kiteAttr -> !KiteStringUtils.isEqual("H", kiteAttr.getShowType()) && !KiteStringUtils
                    .isEqual("1", kiteAttr.getObjId())).collect(Collectors.toList());
        for (KiteAttr kiteAttr : custManagerKiteAttrs) {
            if (KiteStringUtils.equals(LocalKeyConsts.CUST_MANAGER_NAME, kiteAttr.getFieldName()) || KiteStringUtils
                .equals(LocalKeyConsts.CUST_MANAGER_NAME_NEW, kiteAttr.getFieldName())) {
                custManagerName = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
            if (KiteStringUtils.equals(LocalKeyConsts.CUST_MANAGER_PHONE, kiteAttr.getFieldName()) || KiteStringUtils
                .equals(LocalKeyConsts.CUST_MANAGER_PHONE_NEW, kiteAttr.getFieldName())) {
                custManagerPhone = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
            if (KiteStringUtils.equals(LocalKeyConsts.CUST_MANAGER_EMAIL, kiteAttr.getFieldName()) || KiteStringUtils
                .equals(LocalKeyConsts.CUST_MANAGER_EMAIL_NEW, kiteAttr.getFieldName())) {
                custManagerEmail = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
        }
        List<Map<String, Object>> orderInfoList = new ArrayList();
        Map<String, Object> orderInfo = new HashMap<>();
        orderInfo.put("serialNumber", serialNumber); // 业务号码
        orderInfo.put("custName", custName); // 客户名称
        orderInfo.put("city", city); // 所在城市
        orderInfo.put("custManagerName", custManagerName); //客户经理姓名
        orderInfo.put("custManagerPhone", custManagerPhone); //客户经理联系电话
        orderInfo.put("custManagerEmail", custManagerEmail); //客户经理联系邮箱
        /** 订单信息end */

        /** 物联网卡订单信息 start */
        String deliveryName = ""; //收货人
        String deliveryPhone = ""; //收货人电话
        String deliveryAddress = ""; //收货地址
        String provCode = null;
        String cityCode = null;
        String countyCode = null;
        List<Map<String, Object>> deliveryInfos = querySceneInstService
            .getExtParameterValue(sceneInstId, "offerInstDeliveryInfos");
        if (KiteListUtils.isNotEmpty(deliveryInfos)) {
            // 收货信息模块
            List<KiteAttr> deliveryKiteAttrs = (List<KiteAttr>) deliveryInfos.get(deliveryInfos.size() - 1).get("kiteAttrs");
            for (KiteAttr kiteAttr : deliveryKiteAttrs) {
                if (KiteStringUtils.equals("ORD10037", kiteAttr.getFieldName())) {
                    deliveryName = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
                }
                if (KiteStringUtils.equals("ORD10038", kiteAttr.getFieldName())) {
                    deliveryPhone = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
                }
                if (KiteStringUtils.equals("ORD10053", kiteAttr.getFieldName())) {
                    provCode = kiteAttr.getValue();
                }
                if (KiteStringUtils.equals("ORD10054", kiteAttr.getFieldName())) {
                    cityCode = kiteAttr.getValue();
                }
                if (KiteStringUtils.equals("ORD10055", kiteAttr.getFieldName())) {
                    countyCode = kiteAttr.getValue();
                }
                if (KiteStringUtils.equals("ORD10040", kiteAttr.getFieldName())) {
                    deliveryAddress = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
                }
            }
        }


        if (KiteStringUtils.isNotEmpty(provCode)) {
            StringBuffer regionCode = new StringBuffer(provCode);
            if (KiteStringUtils.isNotEmpty(cityCode)) {
                regionCode.append(SymbolConsts.COMMA).append(cityCode);
                if (KiteStringUtils.isNotEmpty(countyCode)) {
                    regionCode.append(SymbolConsts.COMMA).append(countyCode);
                }
            }

            String regionName = LocalAttrHelper.transformRegionCodeToName(regionCode.toString(), null, "");
            if (KiteStringUtils.isNotEmpty(regionName)) {
                deliveryAddress = regionName + deliveryAddress;
            }
        }

        orderInfo.put("deliveryName", deliveryName); //收货人
        orderInfo.put("deliveryPhone", deliveryPhone); //收货人电话
        orderInfo.put("deliveryAddress", deliveryAddress); //收货地址
        /** 物联网卡订单信息 end */

        // 子产品列表
//        List<Map<String, Object>> subSerialNumberList = new ArrayList<Map<String, Object>>(); // 子产品名称号码列表
        List<Map<String, Object>> subInstList = new ArrayList<>();
        List<KiteOfferInst> mainOfferInstList = querySceneInstService.listMainOfferInsts(sceneInstId);
        // 融合主产品实例
        KiteOfferInst offerInst = querySceneInstService.getGiftOfferInst(sceneInstId);
        orderInfo.put("mainProdName", offerInst.getSpecName()); // 主产品名称
        for (KiteOfferInst mainOfferInst : mainOfferInstList) {
            String offerInstId = "";
            if (Objects.nonNull(mainOfferInst)) {
                offerInstId = mainOfferInst.getOfferInstId();
            }
            // 获取销售品实例的所有接入产品实例（融合子产品）
            List<KiteProdInst> prodInsts = querySceneInstService
                .listProdInstsFromOfferInst(currentCust.getCustId(), offerInstId);
            for (KiteProdInst prodInst : prodInsts) {
                String cateId = ProductCache.get(prodInst.getSpecId()).getCateId();
                // 过滤掉附加产品以及资费计划产品实例
                if (GoodsInfoTransfer.isAddRoleOffer(mainOfferInst.getSpecId(), cateId) ||
                    KiteStringUtils.equals(LocalKeyConsts.PROD_USE_TYPE_2000, ProductCache.get(prodInst.getSpecId()).getProdUseType())) {
                    continue;
                }
                Map<String, Object> subInst = new HashMap<String, Object>();
                // 判断如果是连接类产品和非连接类产品,走不同逻辑
                if (KiteStringUtils.equalsAny(prodInst.getSpecId(), ProdCateUtil.getMainMemberProdIdForArr(ProdCateUtil.GIFT_DZ4G_PROD,
                    ProdCateUtil.GIFT_DZCAT1_PROD, ProdCateUtil.GIFT_TYCAT1_PROD, ProdCateUtil.GIFT_DZ5G_PROD, ProdCateUtil.GIFT_TYNB_PROD, ProdCateUtil.GIFT_ZLZXCAT_PROD))) {
                    dealCommonProd(subInst, prodInst, sceneInstId, deliveryName, deliveryPhone, deliveryAddress,
                        currentCust, "2", orderInfo, mainOfferInst.getSpecName());
                    if (KiteMapUtils.isNotEmpty(subInst)) {
                        subInstList.add(subInst);
                    }
                } else { // 非定制类产品
                    subInst.put("prodName", mainOfferInst.getSpecName()); // 子产品名称
                    Map<String, KiteAttr> prodAttrMap = prodInst.getAttrMap();
                    subInst.put("attrList", getAttrList(prodAttrMap)); // 子产品属性
                    subInstList.add(subInst);
                }
                // 处理订单属性
//                Map<String, Object> subSerialNumberMap = new HashMap<String, Object>();
//                subSerialNumberMap.put("name", mainOfferInst.getSpecName());
//                KiteAttr accAttr = !KiteObjectUtils.isEmpty(prodInst) ? prodInst.getAttr("200002981") : null;
//                subSerialNumberMap.put("serialNumber", !KiteObjectUtils.isEmpty(accAttr) ? accAttr.getValue() : "");
//                subSerialNumberList.add(subSerialNumberMap);
            }
        }
//        orderInfo.put("subSerialNumberList", subSerialNumberList);
        orderInfoList.add(orderInfo);
        result.put("orderInfo", orderInfoList);
        result.put("subInstList", subInstList); // 子产品名称及属性列表

        //备注
        String commonAttrValue = querySceneInstService
            .getCommonAttrValue(sceneInstId, LocalKeyConsts.COMMON_ATTR_KEY_REMARK);
        List<Map<String, Object>> remarkList = new ArrayList();
        Map<String, Object> remark = new HashMap();
        remark.put("commonRemaek", commonAttrValue); //备注
        remarkList.add(remark);
        result.put("remaek", remarkList);

        return result;
    }

    @Override
    public Map<String, Object> dealPrintTemplateSingular(String sceneInstId, String serviceOfferId,
                                                         ContextCustDTO currentCust) {
        Map<String, Object> result = new HashMap();

        String serialNumber = ""; // 业务号码
        String custName = ""; // 客户名称
        String city = ""; // 所在城市
        // 集团用户信息模块
        List<KiteAttr> groupUserkiteAttrs = querySceneInstService
            .listCommonAttrsByModuleId(sceneInstId, LocalKeyConsts.GROUPUSERINFO_MODULEID).stream().filter(
                kiteAttr -> !KiteStringUtils.isEqual("H", kiteAttr.getShowType()) && !KiteStringUtils
                    .isEqual("1", kiteAttr.getObjId())).collect(Collectors.toList());
        for (KiteAttr kiteAttr : groupUserkiteAttrs) {
            if (KiteStringUtils.equals("business_number", kiteAttr.getFieldName())) {
                serialNumber = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
            if (KiteStringUtils.equals("user_location", kiteAttr.getFieldName())) {
                city = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
        }
        // bug 138 定制类订购，补录页面，点击配置单打印，订单信息中客户名称字段显示的为用户名称 by 曹婷婷
        KitePosCustomerDTO custInfo = querySceneInstService.getExtParameterValue(sceneInstId, "customer");
        if (Objects.nonNull(custInfo)) {
            custName = custInfo.getName();
        } else {
            custName = currentCust.getCustName();
        }

        String custManagerName = ""; //客户经理姓名
        String custManagerPhone = ""; //客户经理联系电话
        String custManagerEmail = ""; //客户经理联系邮箱
        // 客户经理信息模块
        List<KiteAttr> custManagerKiteAttrs = querySceneInstService
            .listCommonAttrsByModuleId(sceneInstId, LocalKeyConsts.CUSTMANAGERINFO_MODULEID).stream().filter(
                kiteAttr -> !KiteStringUtils.isEqual("H", kiteAttr.getShowType()) && !KiteStringUtils
                    .isEqual("1", kiteAttr.getObjId())).collect(Collectors.toList());
        for (KiteAttr kiteAttr : custManagerKiteAttrs) {
            if (KiteStringUtils.equals(LocalKeyConsts.CUST_MANAGER_NAME, kiteAttr.getFieldName()) || KiteStringUtils
                .equals(LocalKeyConsts.CUST_MANAGER_NAME_NEW, kiteAttr.getFieldName())) {
                custManagerName = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
            if (KiteStringUtils.equals(LocalKeyConsts.CUST_MANAGER_PHONE, kiteAttr.getFieldName()) || KiteStringUtils
                .equals(LocalKeyConsts.CUST_MANAGER_PHONE_NEW, kiteAttr.getFieldName())) {
                custManagerPhone = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
            if (KiteStringUtils.equals(LocalKeyConsts.CUST_MANAGER_EMAIL, kiteAttr.getFieldName()) || KiteStringUtils
                .equals(LocalKeyConsts.CUST_MANAGER_EMAIL_NEW, kiteAttr.getFieldName())) {
                custManagerEmail = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
        }

        String deliveryName = ""; //收货人
        String deliveryPhone = ""; //收货人电话
        String deliveryAddress = ""; //收货地址
        String provCode = null;
        String cityCode = null;
        String countyCode = null;
        // 收货信息模块
        List<KiteAttr> deliveryKiteAttrs = querySceneInstService
            .listCommonAttrsByModuleId(sceneInstId, LocalKeyConsts.DELIVERYINFO_MODULEID).stream().filter(
                kiteAttr -> !KiteStringUtils.isEqual("H", kiteAttr.getShowType()) && !KiteStringUtils
                    .isEqual("1", kiteAttr.getObjId())).collect(Collectors.toList());
        for (KiteAttr kiteAttr : deliveryKiteAttrs) {
            if (KiteStringUtils.equals("ORD10037", kiteAttr.getFieldName())) {
                deliveryName = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
            if (KiteStringUtils.equals("ORD10038", kiteAttr.getFieldName())) {
                deliveryPhone = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
            if (KiteStringUtils.equals("ORD10053", kiteAttr.getFieldName())) {
                provCode = kiteAttr.getValue();
            }
            if (KiteStringUtils.equals("ORD10054", kiteAttr.getFieldName())) {
                cityCode = kiteAttr.getValue();
            }
            if (KiteStringUtils.equals("ORD10055", kiteAttr.getFieldName())) {
                countyCode = kiteAttr.getValue();
            }
            if (KiteStringUtils.equals("ORD10040", kiteAttr.getFieldName())) {
                deliveryAddress = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
        }

        if (KiteStringUtils.isNotEmpty(provCode)) {
            StringBuffer regionCode = new StringBuffer(provCode);
            if (KiteStringUtils.isNotEmpty(cityCode)) {
                regionCode.append(SymbolConsts.COMMA).append(cityCode);
                if (KiteStringUtils.isNotEmpty(countyCode)) {
                    regionCode.append(SymbolConsts.COMMA).append(countyCode);
                }
            }

            String regionName = LocalAttrHelper.transformRegionCodeToName(regionCode.toString(), null, "");
            if (KiteStringUtils.isNotEmpty(regionName)) {
                deliveryAddress = regionName + deliveryAddress;
            }
        }

        // 获取套餐销售品实例
        List<KiteOfferInst> mainOfferInstList = querySceneInstService.listMainOfferInsts(sceneInstId);
        KiteOfferInst mainOfferInst = mainOfferInstList.stream().filter(
            dto -> !GoodsInfoTransfer.isAddRoleOffer(dto.getOfferId(), OfferCache.get(dto.getOfferId()).getCateId()))
            .findFirst().orElse(null);

        String offerInstId = "";
        if (Objects.nonNull(mainOfferInst)) {
            offerInstId = mainOfferInst.getOfferInstId();
        }

        // 获取销售品实例的所有接入产品实例
        List<KiteProdInst> prodInsts = querySceneInstService
            .listProdInstsFromOfferInst(currentCust.getCustId(), offerInstId);

        //备注
        String commonAttrValue = querySceneInstService
            .getCommonAttrValue(sceneInstId, LocalKeyConsts.COMMON_ATTR_KEY_REMARK);

        Map<String, Object> orderInfo = new HashMap<String, Object>();
        List<Map<String, Object>> orderInfoList = new ArrayList();
        orderInfo.put("serialNumber", serialNumber); // 业务号码
        orderInfo.put("custName", custName); // 客户名称
        orderInfo.put("city", city); // 所在城市
        orderInfo.put("custManagerName", custManagerName); //客户经理姓名
        orderInfo.put("custManagerPhone", custManagerPhone); //客户经理联系电话
        orderInfo.put("custManagerEmail", custManagerEmail); //客户经理联系邮箱

        List<Map<String, Object>> subInstList = Lists.newArrayList();
        for (KiteProdInst prodInst : prodInsts) {
            if (KiteStringUtils.isEmpty(ProductCache.get(prodInst.getSpecId()).getCateId()) || GoodsInfoTransfer
                .isAddRoleOffer(mainOfferInst.getSpecId(), ProductCache.get(prodInst.getSpecId()).getCateId())) {
                continue;
            }
            Map<String, Object> subInst = new HashMap<String, Object>();
            dealCommonProd(subInst, prodInst, sceneInstId, deliveryName, deliveryPhone, deliveryAddress, currentCust,
                "1", orderInfo, mainOfferInst.getSpecName());
            if (KiteMapUtils.isNotEmpty(subInst)) {
                subInstList.add(subInst);
            }
        }
        orderInfoList.add(orderInfo);
        result.put("orderInfo", orderInfoList);
        result.put("subInstList", subInstList);

        // 组装备注
        List<Map<String, Object>> remarkList = new ArrayList();
        Map<String, Object> remark = new HashMap();
        remark.put("commonRemaek", commonAttrValue); //备注
        remarkList.add(remark);
        result.put("remaek", remarkList);

        return result;
    }

    private void dealCommonProd(Map<String, Object> subInst, KiteProdInst prodInst, String sceneInstId,
                                String deliveryName, String deliveryPhone, String deliveryAddress, ContextCustDTO currentCust,
                                String orderInfoFlag, Map<String, Object> orderInfo, String prodName) {
        // 网络类型
        String specName = "";
        // 承载平台
        String carryingPlatform = "";
        // 业务场景
        String businessScene = "";
        // 管理员姓名
        String adminName = "";
        // 管理员电话
        String adminPhone = "";
        // 管理员邮箱
        String adminEmail = "";
        // SIM卡类型
        String simCardType = "";
        // 号卡数据服务费
        String cardDataServiceFee = "";
        // 企业自采
        String firmSelf = "";
        // 订购数量
        String buyNum = "";
        // 平台服务级别
        String serviceLevel = "";
        // 平台服务级别
        String serviceLevelName = "";
        // 平台功能费
        String functionFee = "";
        // 资费计划标识(资费计划服务信息)
        String tariffPlanMarkService = "";
        // 通信套包类型
        String packageType = "";
        // 周期用量限制
        String useLimit = "";
        // 区域编码
        String areaCode = "";
        // 套内用量
        String setUseNum = "";
        // 套包价格
        String setPrice = "";
        // 超出资费
        String overFee = "";
        // 折扣类型
        String discountType = "";
        // 最终订购费
        String endBugFee = "";
        // SIM卡计费带宽限期(天)
        String simBandwidth = "";
        // SIM卡激活起最短连续计费期限(月)
        String simActivaShort = "";
        // 激活首月订购费收费方式
        String firstMonthFee = "";
        // SIM卡激活承诺期(天)
        String simNewwidth = "";
        // SIM卡续约计划
        String simPlan = "";
        //SIM 到期续约模式
        String RenewalMode = "";
        //订单信息
        Map<String, KiteAttr> prodAttrMap = prodInst.getAttrMap();
        specName = prodAttrMap.get("200003674") == null ? "" : prodAttrMap.get("200003674").getValueDesc(); // 网络类型
        carryingPlatform = prodAttrMap.get("200003675") == null ? "" : prodAttrMap.get("200003675").getValueDesc(); // 承载平台
        businessScene = prodAttrMap.get("200001679") == null ? "" : prodAttrMap.get("200001679").getValueDesc(); // 业务场景
        adminName = prodAttrMap.get("200003993") == null ? "" : prodAttrMap.get("200003993").getValueDesc(); // 管理员姓名
        adminPhone = prodAttrMap.get("200003994") == null ? "" : prodAttrMap.get("200003994").getValueDesc(); // 管理员电话
        adminEmail = prodAttrMap.get("200003995") == null ? "" : prodAttrMap.get("200003995").getValueDesc(); // 管理员邮箱
        simCardType = prodAttrMap.get("200003678") == null ? "" : prodAttrMap.get("200003678").getValueDesc(); // SIM卡类型
        cardDataServiceFee = prodAttrMap.get("200003679") == null ? "" : prodAttrMap.get("200003679").getValueDesc(); // 号卡数据服务费
        firmSelf = prodAttrMap.get("200003676") == null ? "" : prodAttrMap.get("200003676").getValueDesc(); // 企业自采
        buyNum = prodAttrMap.get("200001696") == null ? "" : prodAttrMap.get("200001696").getValueDesc(); // 订购数量

        if (KiteStringUtils.isNotEmpty(carryingPlatform)) {
            List<KiteAttrValue> carryingPlatformAttrValues = prodAttrMap.get("200003675").getAttrValues();
            if (KiteListUtils.isNotEmpty(carryingPlatformAttrValues)) {
                carryingPlatform = transAttrValueDesc(carryingPlatformAttrValues, carryingPlatform);
            }
        }
        if (KiteStringUtils.isNotEmpty(firmSelf)) {
            List<KiteAttrValue> firmSelfAttrValues = prodAttrMap.get("200003676").getAttrValues();
            if (KiteListUtils.isNotEmpty(firmSelfAttrValues)) {
                firmSelf = transAttrValueDesc(firmSelfAttrValues, firmSelf);
            }
        }

        //功能产品信息
        String prodInstId = prodInst.getProdInstId();
        // 【通信计划定制套包产品】根据功能产品id获取接入产品实例的功能产品实例
        List<KiteProdInstFunc> newsProdInstFuncs = (List<KiteProdInstFunc>) querySceneInstService
            .listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST_FUNC)).stream().filter(
                inst -> KiteStringUtils
                    .equalsAny(ProductCache.get(((KiteProdInstFunc) inst).getSpecId()).getProdNbr(), "300000151", LocalKeyConsts.FUNC_PROD_ID_YF5GTX))
            .collect(Collectors.toList());
        if (KiteListUtils.isEmpty(newsProdInstFuncs)) {
            //取不到则取产品本身的分组
            newsProdInstFuncs = Lists.newArrayList();
            Map<String, KiteAttr> attrMap = this.getGroupAttrs(prodAttrMap, LocalKeyConsts.FUNC_PROD_ID_TX);
            KiteProdInstFunc funcProdInst = new KiteProdInstFunc();
            funcProdInst.setAttrMap(attrMap);
            newsProdInstFuncs.add(funcProdInst);
        }
        if (KiteListUtils.isNotEmpty(newsProdInstFuncs)) {
            List<Map<String, Object>> newsFunctionList = new ArrayList();
            for (KiteProdInstFunc funcProdInst : newsProdInstFuncs) {
                if (KiteStringUtils.equalsAny(funcProdInst.getActionType().name(), ActionType.D.name(), ActionType.X.name())) {
                    continue;
                }
                // 是否默认通讯计划
                String isMoRen = "";
                // 通讯计划名称
                String newsFunctionName = "";
                // APN设置
                String apnSet = "";
                // 漫游区域
                String roamArea = "";
                // 通讯类型
                String newsType = "";
                // 开通4G
                String open4G = "开通4G";
                Map<String, Object> newsFunction = new HashMap();
                // 通信功能
                Map<String, KiteAttr> newsFunctionAttrMap = funcProdInst.getAttrMap();
                //是否默认通信计划
                isMoRen = newsFunctionAttrMap.get("200004020") == null ?
                    "" :
                    newsFunctionAttrMap.get("200004020").getValueDesc();
                //通信计划名称
                newsFunctionName = newsFunctionAttrMap.get("200003687") == null ?
                    "" :
                    newsFunctionAttrMap.get("200003687").getValueDesc();
                //apn设置
                apnSet =
                    newsFunctionAttrMap.get("200004034") == null ? "" : newsFunctionAttrMap.get("200004034").getValue();
                if (KiteStringUtils.isEmpty(apnSet)) {
                    apnSet = newsFunctionAttrMap.get("200004078") == null ?
                        "" :
                        newsFunctionAttrMap.get("200004078").getValueDesc();
                }
                //漫游区域
                roamArea = newsFunctionAttrMap.get("200003690") == null ?
                    "" :
                    newsFunctionAttrMap.get("200003690").getValueDesc();
                if (KiteStringUtils.isEmpty(roamArea)) {
                    roamArea = newsFunctionAttrMap.get("200004080") == null ?
                        "" :
                        newsFunctionAttrMap.get("200004080").getValueDesc();
                }
                //通讯类型
                newsType = newsFunctionAttrMap.get("200003688") == null ?
                    "" :
                    newsFunctionAttrMap.get("200003688").getValueDesc();
                if (KiteStringUtils.isEmpty(newsType)) {
                    newsType += this.getAttrNameAndValue(newsFunctionAttrMap, "200004075");
                    newsType += this.getAttrNameAndValue(newsFunctionAttrMap, "200004076");
                    newsType += this.getAttrNameAndValue(newsFunctionAttrMap, "200004077");
                }
                // 转换编码为中文
                if (KiteStringUtils.isNotEmpty(newsFunctionName)) {
                    List<KiteAttrValue> newsFunctionNameAttrValues = newsFunctionAttrMap.get("200003687")
                        .getAttrValues();
                    if (KiteListUtils.isNotEmpty(newsFunctionNameAttrValues)) {
                        newsFunctionName = transAttrValueDesc(newsFunctionNameAttrValues, newsFunctionName);
                        // 如果是否默认通讯计划 ”是”，通讯计划名称后加(默认)
                        if (KiteStringUtils.equals("1_0000106", isMoRen)) {
                            newsFunctionName = newsFunctionName + "(默认)";
                        }
                    }
                }
                if (KiteStringUtils.isNotEmpty(roamArea)) {
                    KiteAttr roamAreaAttr = Optional.ofNullable(newsFunctionAttrMap.get("200003690"))
                        .orElse(newsFunctionAttrMap.get("200004080"));
                    List<KiteAttrValue> roamAreaAttrValues = roamAreaAttr.getAttrValues();
                    if (KiteListUtils.isNotEmpty(roamAreaAttrValues)) {
                        roamArea = transAttrValueDesc(roamAreaAttrValues, roamArea);
                    }
                }
                if (KiteStringUtils.isNotEmpty(newsType) && !KiteObjectUtils
                    .isEmpty(newsFunctionAttrMap.get("200003688"))) {
                    List<KiteAttrValue> newsTypeAttrValues = newsFunctionAttrMap.get("200003688").getAttrValues();
                    if (KiteListUtils.isNotEmpty(newsTypeAttrValues)) {
                        newsType = transAttrValueDesc(newsTypeAttrValues, newsType);
                    }
                }
                // 组装通信功能
                newsFunction.put("newsFunctionName", newsFunctionName); // 通讯计划名称
                newsFunction.put("apnSet", apnSet); // APN设置
                newsFunction.put("roamArea", roamArea); // 漫游区域
                newsFunction.put("newsType", newsType); // 通讯类型
                newsFunction.put("open4G", open4G); // 开通4G
                newsFunctionList.add(newsFunction);
            }
            subInst.put("newsFunction", newsFunctionList);
        }

        // 【APN定制套包产品】根据功能产品id获取接入产品实例的功能产品实例
        List<KiteProdInstFunc> apnProdInstFuncs = (List<KiteProdInstFunc>) querySceneInstService
            .listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST_FUNC)).stream().filter(
                inst -> KiteStringUtils.equalsAny(ProductCache.get(((KiteProdInstFunc) inst).getSpecId()).getProdNbr(),
                    LocalKeyConsts.FUNC_PROD_ID_APN, LocalKeyConsts.FUNC_PROD_ID_YF5GAPN)).collect(Collectors.toList());
        if (KiteListUtils.isEmpty(apnProdInstFuncs)) {
            //取不到则取产品本身的分组
            apnProdInstFuncs = Lists.newArrayList();
            Map<String, KiteAttr> attrMap = this.getGroupAttrs(prodAttrMap, LocalKeyConsts.FUNC_PROD_ID_APN);
            KiteProdInstFunc funcProdInst = new KiteProdInstFunc();
            funcProdInst.setAttrMap(attrMap);
            apnProdInstFuncs.add(funcProdInst);
        }
        if (KiteListUtils.isNotEmpty(apnProdInstFuncs)) {
            List<Map<String, Object>> apnInfoList = new ArrayList();
//            List<Map<String, Object>> customApnInfoList = new ArrayList();
            for (KiteProdInstFunc funcProdInst : apnProdInstFuncs) {
                if (KiteStringUtils.equalsAny(funcProdInst.getActionType().name(), ActionType.D.name(), ActionType.X.name())) {
                    continue;
                }
                // 共享APN域名
                String shareApn = "";
                // 新增APN方式
                String addApnWay = "";
                // APN标识
                String apnMark = "";
                // 终端使用固定IP
                String useFixIlp = "";
                // APN定制信息 APN名称
                String customApnName = "";
                // APN类型
                String customApnType = "";
                // 细分业务类型
                String customApnBusiness = "";
                // 操作类型
                String customApnOperateType = "";
                // 使用PCRF
                String PCRF = "";
                // apnIp地址
                String apnIp = "";
                // apnURL
                String apnUrl = "";
                // APN定制信息 备注
                String customApnRemark = "";
                Map<String, Object> apnInfo = new HashMap();
//                Map<String, Object> customApnInfo = new HashMap();
                // APN
                Map<String, KiteAttr> attrMap = funcProdInst.getAttrMap();
                //共享APN域名  5GSCUIOT（自建）
                shareApn = attrMap.get("200002833") == null ? "" : attrMap.get("200002833").getValueDesc();
                if (KiteStringUtils.isEmpty(shareApn)) {
                    shareApn = attrMap.get("200003997") == null ? "" : attrMap.get("200003997").getValueDesc();
                }
                //新增APN方式
                addApnWay = attrMap.get("200003996") == null ? "" : attrMap.get("200003996").getValueDesc();
                if (KiteStringUtils.isEmpty(addApnWay)) {
                    //addApnWay = attrMap.get("200003999") == null ? "" : attrMap.get("200003999").getValueDesc();
                }
                //终端使用固定IP
                useFixIlp = attrMap.get("200004014") == null ? "" : attrMap.get("200004014").getValueDesc();
                if (KiteStringUtils.isEmpty(useFixIlp)) {
                    useFixIlp = attrMap.get("200004084") == null ? "" : attrMap.get("200004084").getValueDesc();
                }
                // 新增APN方式
                // 100006950(使用共享APN) APN信息展示：共享APN域名 新增APN方式 终端使用固定IP
                // 100006951(使用已创建APN) APN信息展示：复用APN域名 新增APN方式 终端使用固定IP
                // 100006952(我要定制) APN信息展示：APN标识 新增APN方式 终端使用固定IP
                if (KiteStringUtils.equals("100006950", shareApn)) {
                    apnMark = attrMap.get("200003997") == null ? "" : attrMap.get("200003997").getValueDesc();
                } else if (KiteStringUtils.equals("100006951", shareApn)) {
                    apnMark = attrMap.get("200003998") == null ? "" : attrMap.get("200003998").getValueDesc();
                } else if (KiteStringUtils.equals("100006952", shareApn)) {
                    apnMark = attrMap.get("200004135") == null ? "" : attrMap.get("200004135").getValueDesc();
                }
                if (KiteStringUtils.isEmpty(apnMark)) {
                    KiteAttr apnMarkAttr = Optional.ofNullable(attrMap.get("200004135"))
                        .orElse(attrMap.get("200004104"));
                    apnMark = apnMarkAttr == null ? "" : apnMarkAttr.getValueDesc();
                }
                // 转换编码为中文
                if (KiteStringUtils.isNotEmpty(addApnWay)) {
                    List<KiteAttrValue> addApnWayAttrValues = attrMap.get("200003996").getAttrValues();
                    if (KiteListUtils.isNotEmpty(addApnWayAttrValues)) {
                        addApnWay = transAttrValueDesc(addApnWayAttrValues, addApnWay);
                    }
                }
                if (KiteStringUtils.isNotEmpty(apnMark) && attrMap.get("200004135") != null) {
                    List<KiteAttrValue> ApnMarkAttrValues = attrMap.get("200004135").getAttrValues();
                    if (KiteListUtils.isNotEmpty(ApnMarkAttrValues)) {
                        apnMark = transAttrValueDesc(ApnMarkAttrValues, apnMark);
                    }
                }
                if (KiteStringUtils.isNotEmpty(useFixIlp)) {
                    KiteAttr attr = Optional.ofNullable(attrMap.get("200004014")).orElse(attrMap.get("200004084"));
                    List<KiteAttrValue> useFixIlpAttrValues = attr.getAttrValues();
                    if (KiteListUtils.isNotEmpty(useFixIlpAttrValues)) {
                        useFixIlp = transAttrValueDesc(useFixIlpAttrValues, useFixIlp);
                    }
                }

                //定制APN信息
                customApnName = attrMap.get("200004135") == null ? "" : attrMap.get("200004135").getValueDesc();
                if (KiteStringUtils.isEmpty(customApnName)) {
                    customApnName = attrMap.get("200004104") == null ? "" : attrMap.get("200004104").getValueDesc();
                }
                customApnType = attrMap.get("200003999") == null ? "" : attrMap.get("200003999").getValueDesc();
                customApnBusiness = attrMap.get("200004001") == null ? "" : attrMap.get("200004001").getValueDesc();
                customApnOperateType = attrMap.get("200002753") == null ? "" : attrMap.get("200002753").getValueDesc();
                PCRF = attrMap.get("200004005") == null ? "" : attrMap.get("200004005").getValueDesc();
                customApnRemark = attrMap.get("200004003") == null ? "" : attrMap.get("200004003").getValueDesc();
                if (KiteStringUtils.isNotBlank(customApnType)) {
                    if (KiteStringUtils.equals(customApnType,"定向白名单")) {
                        apnIp = attrMap.get("200001489") == null ? "" : attrMap.get("200001489").getValueDesc();
                        apnUrl = attrMap.get("200004002") == null ? "" : attrMap.get("200004002").getValueDesc();
                    }
                }
                // 转换编码为中文 -- 已经是中文
                if (KiteStringUtils.isNotEmpty(customApnType)) {
                    List<KiteAttrValue> customApnTypeAttrValues = attrMap.get("200003999").getAttrValues();
                    if (KiteListUtils.isNotEmpty(customApnTypeAttrValues)) {
                        customApnType = transAttrValueDesc(customApnTypeAttrValues, customApnType);
                    }
                } else {
                    customApnType = attrMap.get("200003996") == null ? "" : attrMap.get("200003996").getValueDesc();
                }
                if (KiteStringUtils.isNotEmpty(customApnBusiness)) {
                    List<KiteAttrValue> customApnBusinessAttrValues = attrMap.get("200004001").getAttrValues();
                    if (KiteListUtils.isNotEmpty(customApnBusinessAttrValues)) {
                        customApnBusiness = transAttrValueDesc(customApnBusinessAttrValues, customApnBusiness);
                    }
                }
                if (KiteStringUtils.isNotEmpty(customApnOperateType)) {
                    List<KiteAttrValue> customApnOperateTypeAttrValues = attrMap.get("200002753").getAttrValues();
                    if (KiteListUtils.isNotEmpty(customApnOperateTypeAttrValues)) {
                        customApnOperateType = transAttrValueDesc(customApnOperateTypeAttrValues, customApnOperateType);
                    }
                }
                if (KiteStringUtils.isNotEmpty(PCRF)) {
                    List<KiteAttrValue> PCRFAttrValues = attrMap.get("200004005").getAttrValues();
                    if (KiteListUtils.isNotEmpty(PCRFAttrValues)) {
                        PCRF = transAttrValueDesc(PCRFAttrValues, PCRF);
                    }
                }
                // 组装APN信息
                apnInfo.put("shareApn", shareApn); // 共享APN域名
                apnInfo.put("addApnWay", addApnWay); // 新增APN方式
                apnInfo.put("apnMark", apnMark); // APN标识
                apnInfo.put("useFixIlp", useFixIlp); // 终端使用固定IP
                if (!KiteStringUtils.isEmpty(customApnType)) {
                    apnInfo.put("customApnType", customApnType);// APN类型
                }
                if (!KiteStringUtils.isEmpty(apnIp)) {
                    apnInfo.put("apnIp", apnIp);
                }
                if (!KiteStringUtils.isEmpty(apnUrl)) {
                    apnInfo.put("apnUrl", apnUrl);
                }
                if (!KiteStringUtils.isEmpty(addApnWay) && KiteStringUtils
                    .equals(addApnWay, LocalKeyConsts.LOCAL_APN_ADD_WAY_WYDZ)) {
                    apnInfo.put("customApnBusiness", customApnBusiness); // 细分业务类型
                    apnInfo.put("customApnOperateType", customApnOperateType); // 操作类型
                    apnInfo.put("PCRF", PCRF); // 使用PCRF
                    apnInfo.put("customApnRemark", customApnRemark); // APN定制信息 备注
                }
                apnInfoList.add(apnInfo);
//                if (!KiteStringUtils.isEmpty(addApnWay) && KiteStringUtils
//                    .equals(addApnWay, LocalKeyConsts.LOCAL_APN_ADD_WAY_WYDZ)) {
//                    //组装定制APN信息
//                    customApnInfo.put("customApnName", customApnName); // APN定制信息 APN名称
//                    customApnInfo.put("customApnType", customApnType); // APN类型
//                    customApnInfo.put("customApnBusiness", customApnBusiness); // 细分业务类型
//                    customApnInfo.put("customApnOperateType", customApnOperateType); // 操作类型
//                    customApnInfo.put("PCRF", PCRF); // 使用PCRF
//                    customApnInfo.put("customApnRemark", customApnRemark); // APN定制信息 备注
//                    customApnInfoList.add(customApnInfo);
//                }
            }
            subInst.put("apnInfo", apnInfoList);
//            subInst.put("customApnInfo", customApnInfoList);
        }

        // 资费计划
        Map<String, Object> tariffMap = new HashMap<>();
        List<Map<String, String>> tariffTopAttrList = new ArrayList<>();
        // 平台服务级别
        addTariffAttrInfo(tariffTopAttrList, prodInst.getAttr("200003681"));
        // 平台功能费(%)
        addTariffAttrInfo(tariffTopAttrList, prodInst.getAttr("200004024"));
        // 资费基础信息
        tariffMap.put("tariffTopAttrList", tariffTopAttrList);

        List<Map<String, Object>> tariffAttrList = new ArrayList<>();
        List<KiteProdInst> prodInsts = querySceneInstService.listProdInsts(sceneInstId);
        // 当前所订购的所有资费套包类型
        List<String> feePkgTypes = new ArrayList<>();
        for (KiteProdInst inst : prodInsts) {
            List<KiteProdInstFunc> kiteProdInstFuncs = querySceneInstService.listProdInstFuncs(currentCust.getCustId(), inst.getProdInstId());
            if (KiteListUtils.isEmpty(kiteProdInstFuncs)) {
                continue;
            }

            Map<String, Object> tariffAttrInfo = new HashMap<>();
            List<Map<String, String>> tariffTypeAttrList = new ArrayList<>();
            List<List<Map<String, String>>> tariffAreaModuleList = new ArrayList<>();

            // 资费-基础信息
            KiteProdInstFunc baseInstFunc = kiteProdInstFuncs.stream().filter(temp ->
                KiteStringUtils.equalsAny(temp.getProdName(), "基本信息") && Objects.nonNull(temp.getAttr("200003699"))).findAny().orElse(null);
            if (Objects.nonNull(baseInstFunc)) {
                // 资费计划标识
                addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200004129"));
                // 资费套包类型
                addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200003697"));
                // 周期用量限制(M/月)
                addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200003699"));
                // 期限（天）
                addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200003700"));
                Optional.ofNullable(baseInstFunc.getAttr("200003697")).map(KiteAttr::getValue).ifPresent(feePkgTypes::add);
            } else {
                baseInstFunc = kiteProdInstFuncs.stream().filter(prodInstFunc -> Objects.nonNull(prodInstFunc.getAttr("200004074"))).findAny().orElse(null);
                if (Objects.nonNull(baseInstFunc)) {
                    // 流量套包
                    addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200004074"));
                    // 资费套包类型
                    addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200003697"));
                    // 套内用量
                    addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200004083"));
                    // 套包价格(元/期限)
                    addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200003704"));
                    // 申请折扣后价格(元)
                    addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200004082"));
                    // 超出资费(元/kb)
                    addTariffAttrInfo(tariffTypeAttrList, baseInstFunc.getAttr("200003705"));
                    Optional.ofNullable(baseInstFunc.getAttr("200003697")).map(KiteAttr::getValue).ifPresent(feePkgTypes::add);
                } else {
                    // top区，增加资费套包类型
                    if (tariffTopAttrList.stream().noneMatch(map -> KiteStringUtils.isEqual(map.get("fieldName"), "200003697"))) {
                        KiteProdInstFunc tempInstFunc = kiteProdInstFuncs.stream().filter(prodInstFunc -> Objects.nonNull(prodInstFunc.getAttr("200003697"))).findAny().orElse(null);
                        if (Objects.nonNull(tempInstFunc)) {
                            addTariffAttrInfo(tariffTopAttrList, tempInstFunc.getAttr("200003697"));
                            if(Objects.nonNull(baseInstFunc)){
                                Optional.ofNullable(baseInstFunc.getAttr("200003697")).map(KiteAttr::getValue).ifPresent(feePkgTypes::add);
                            }
                        }
                    }
                }
                // top区，增加资费计划标识
                if (tariffTopAttrList.stream().noneMatch(map -> KiteStringUtils.isEqual(map.get("fieldName"), "200004129"))) {
                    KiteProdInstFunc tempInstFunc = kiteProdInstFuncs.stream().filter(prodInstFunc -> Objects.nonNull(prodInstFunc.getAttr("200004129"))).findAny().orElse(null);
                    if (Objects.nonNull(tempInstFunc)) {
                        addTariffAttrInfo(tariffTopAttrList, tempInstFunc.getAttr("200004129"));
                    }
                }
            }

            // 资费-折扣信息
            KiteProdInstFunc discountInstFunc = kiteProdInstFuncs.stream().filter(temp ->
                KiteStringUtils.equalsAny(temp.getProdName(), "折扣信息")).findAny().orElse(null);
            if (Objects.nonNull(discountInstFunc)) {
                // 最终订购费(元)
                KiteAttr discountAttr = discountInstFunc.getAttr("200003715");
                if (Objects.isNull(discountAttr) || KiteStringUtils.equalsAny(discountAttr.getShowType(), KeyConsts.PROD_ATTR_SHOW_TYPE_E, KeyConsts.PROD_ATTR_SHOW_TYPE_H)) {
                    discountAttr = discountInstFunc.getAttr("200003717");
                }
                if (Objects.nonNull(discountAttr)) {
                    Map<String, String> tariffAttr = new HashMap<>();
                    tariffAttr.put("fieldName", discountAttr.getFieldName());
                    tariffAttr.put("cname", "最终订购费(元)");

                    String value = discountAttr.getValueDesc();
                    if (KiteStringUtils.isEmpty(value)) {
                        value = transAttrValueDesc(discountAttr.getAttrValues(), discountAttr.getValue());
                    }
                    tariffAttr.put("value", value);
                    tariffTypeAttrList.add(tariffAttr);
                }
            }

            // 资费-资费服务
            List<KiteProdInstFunc> tariffServiceFuncs = kiteProdInstFuncs.stream().filter(temp ->
                KiteStringUtils.equalsAny(temp.getProdName(), "资费服务")).collect(Collectors.toList());
            if (KiteListUtils.isNotEmpty(tariffServiceFuncs)) {
                String basePriceFieldName = Objects.nonNull(baseInstFunc)
                    && KiteStringUtils.equalsAny(baseInstFunc.getAttrValue("200003697"), "100006433", "100006434")
                    ? "210005341" : "200002773";
                tariffServiceFuncs.forEach(serviceInstFunc -> {
                    List<Map<String, String>> tariffAreaAttrList = new ArrayList<>();
                    // 区域编码
                    addTariffAttrInfo(tariffAreaAttrList, serviceInstFunc.getAttr("220002597"));
                    // 区域用量限制(M/月)
                    addTariffAttrInfo(tariffAreaAttrList, serviceInstFunc.getAttr("200004029"));
                    // 套内用量(M)
                    addTariffAttrInfo(tariffAreaAttrList, serviceInstFunc.getAttr("200003703"));
                    // 标准价格(元/月)
                    addTariffAttrInfo(tariffAreaAttrList, serviceInstFunc.getAttr(basePriceFieldName));
                    // 超出资费(元/kb)
                    addTariffAttrInfo(tariffAreaAttrList, serviceInstFunc.getAttr("200003705"));
                    if (tariffAreaAttrList.size() > 0) {
                        tariffAreaModuleList.add(tariffAreaAttrList);
                    }
                });
            }

            if (tariffTypeAttrList.size() > 0) {
                tariffAttrInfo.put("tariffTypeAttrList", tariffTypeAttrList);
            }
            if (tariffAreaModuleList.size() > 0) {
                tariffAttrInfo.put("tariffAreaModuleList", tariffAreaModuleList);
            }
            if (tariffAttrInfo.size() > 0) {
                tariffAttrList.add(tariffAttrInfo);
            }
        }
        if (tariffAttrList.size() > 0) {
            tariffMap.put("tariffAttrList", tariffAttrList);
        }
        subInst.put("tariff", tariffMap);

        // 通信补充套包
        List tariffAddList = new ArrayList();
        List<KiteProdInstFunc> tariffAddFuncs = querySceneInstService.listProdInstFuncsByProdId(currentCust.getCustId(), prodInstId, "300000158");
        if (KiteListUtils.isNotEmpty(tariffAddFuncs)) {
            for (KiteProdInstFunc funcProdInst : tariffAddFuncs) {
                Map<String, Object> tariffPlan = new HashMap();
                Map<String, KiteAttr> attrMap = funcProdInst.getAttrMap();
                // 套包价格
                setPrice = attrMap.get("200003704") == null ? "" : attrMap.get("200003704").getValueDesc();
                // 超出资费
                overFee = attrMap.get("200003705") == null ? (attrMap.get("210005120") == null ? "" : attrMap.get("210005120").getValueDesc()) : attrMap.get("200003705").getValueDesc();
                tariffPlan.put("setPrice", setPrice); // 套包价格
                tariffPlan.put("overFee", overFee); // 超出资费
                tariffAddList.add(tariffPlan);
            }
        }
        subInst.put("tariffAddList", tariffAddList);

        // 【承诺】根据功能产品id获取接入产品实例的功能产品实例
        List<KiteProdInstFunc> promiseProdInstFuncs = (List<KiteProdInstFunc>) querySceneInstService
            .listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST_FUNC)).stream().filter(
                inst -> KiteStringUtils
                    .equals(ProductCache.get(((KiteProdInstFunc) inst).getSpecId()).getProdNbr(), "300000167"))
            .collect(Collectors.toList());
        if (KiteListUtils.isEmpty(promiseProdInstFuncs)) {
            promiseProdInstFuncs = Lists.newArrayList();
            Map<String, KiteAttr> attrMap = this.getGroupAttrs(prodAttrMap, "300000167");
            KiteProdInstFunc funcProdInst = new KiteProdInstFunc();
            funcProdInst.setAttrMap(attrMap);
            promiseProdInstFuncs.add(funcProdInst);
        }

        if (KiteListUtils.isNotEmpty(promiseProdInstFuncs)) {
            for (KiteProdInstFunc funcProdInst : promiseProdInstFuncs) {
                // 承诺
                Map<String, KiteAttr> attrMap = funcProdInst.getAttrMap();
                simBandwidth = attrMap.get("200004031") == null ? "" : attrMap.get("200004031").getValueDesc();
                simActivaShort = attrMap.get("200003684") == null ? "" : attrMap.get("200003684").getValueDesc();
                firstMonthFee = attrMap.get("200003685") == null ? "" : attrMap.get("200003685").getValueDesc();
                simNewwidth = attrMap.get("200004032") == null ? "" : attrMap.get("200004032").getValueDesc();
                simPlan = attrMap.get("200004027") == null ? "" : attrMap.get("200004027").getValueDesc();
                RenewalMode = attrMap.get("200004026") == null ? "" : attrMap.get("200004026").getValueDesc();
                // 转换编码为中文
                if (KiteStringUtils.isNotEmpty(firstMonthFee)) {
                    List<KiteAttrValue> firstMonthFeeAttrValues = attrMap.get("200003685").getAttrValues();
                    if (KiteListUtils.isNotEmpty(firstMonthFeeAttrValues)) {
                        firstMonthFee = transAttrValueDesc(firstMonthFeeAttrValues, firstMonthFee);
                    }
                }
            }
            // 组装承诺
            List<Map<String, Object>> promiseList = new ArrayList();
            Map<String, Object> promise = new HashMap();
            if (feePkgTypes.contains("100006431") || feePkgTypes.contains("100006432") || feePkgTypes.contains("100007469")) {
                promise.put("simBandwidth", simBandwidth); // SIM卡计费带宽限期(天)
                promise.put("simActivaShort", simActivaShort); // SIM卡激活起最短连续计费期限(月)
                promise.put("firstMonthFee", firstMonthFee); // 激活首月订购费收费方式
            }
            if (feePkgTypes.contains("100006433") || feePkgTypes.contains("100006434") || feePkgTypes.contains("100007470")) {
                promise.put("simNewwidth", simNewwidth);// SIM卡激活承诺期(天)
                promise.put("simPlan", simPlan);// SIM卡续约计划
                promise.put("RenewalMode", RenewalMode);//SIM 到期续约模式
            }

            promiseList.add(promise);
            subInst.put("promise", promiseList);
        }
        // 组装物联网卡订单信息
        List<Map<String, Object>> thingscardOrderInfoList = new ArrayList();
        Map<String, Object> thingscardOrderInfo = new HashMap();
        thingscardOrderInfo.put("simCardType", simCardType); // SIM卡类型
        thingscardOrderInfo.put("cardDataServiceFee", cardDataServiceFee); // 号卡数据服务费
        thingscardOrderInfo.put("firmSelf", firmSelf); // 企业自采
        thingscardOrderInfo.put("buyNum", buyNum); // 订购数量
        KiteOfferInst offerInst = querySceneInstService.getGiftOfferInst(sceneInstId);
        // 融合收货信息放到订单信息
        if (Objects.isNull(offerInst)) {
            thingscardOrderInfo.put("deliveryName", deliveryName); //收货人
            thingscardOrderInfo.put("deliveryPhone", deliveryPhone); //收货人电话
            thingscardOrderInfo.put("deliveryAddress", deliveryAddress); //收货地址
        }
        thingscardOrderInfoList.add(thingscardOrderInfo);
        subInst.put("thingscardOrderInfo", thingscardOrderInfoList);
        subInst.put("prodName", prodName);
        // 组装订单信息
        orderInfo.put("adminName", adminName); // 管理员姓名
        orderInfo.put("adminPhone", adminPhone); // 管理员电话
        orderInfo.put("adminEmail", adminEmail); // 管理员邮箱
        orderInfo.put("specName", specName); // 网络类型
        orderInfo.put("carryingPlatform", carryingPlatform); // 承载平台
        orderInfo.put("businessScene", businessScene); // 业务场景
    }

    private static List<Map<String, String>> getAttrList(Map<String, KiteAttr> prodAttrMap) {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> temp;
        for (KiteAttr attr : prodAttrMap.values()) {
            if ("E".equals(attr.getShowType()) || "H".equals(attr.getShowType())) {
                continue;
            }
            temp = new HashMap<>();
            temp.put("name", attr.getCname());
            temp.put("value", KiteStringUtils.isEmpty(attr.getValueDesc()) ?
                (KiteStringUtils.isEmpty(attr.getValue()) ? "" : attr.getValue()) :
                attr.getValueDesc());
            temp.put("code", attr.getFieldName());
            list.add(temp);
        }
        return list;
    }

    private String getAttrNameAndValue(Map<String, KiteAttr> attrMap, String fieldName) {
        String value = "";
        value += attrMap.get(fieldName) == null ? "" : attrMap.get(fieldName).getCname();
        value += attrMap.get(fieldName) == null ? "" : "(" + attrMap.get(fieldName).getValueDesc() + ")";
        return value;
    }

    private String transAttrValueDesc(List<KiteAttrValue> attrValues, String value) {
        if (KiteListUtils.isNotEmpty(attrValues)) {
            for (KiteAttrValue attrValue : attrValues) {
                if (KiteStringUtils.equals(value, attrValue.getAttrValue())) {
                    value = attrValue.getAttrValueName();
                    return value;
                }
            }
        }
        return value;
    }

    private Map<String, KiteAttr> getGroupAttrs(Map<String, KiteAttr> prodAttrMap, String groupId) {
        List<KiteAttr> attrList = new ArrayList<KiteAttr>(prodAttrMap.values());
        Map<String, List<KiteAttr>> grpAttrMap = KiteListUtils.groupBy(attrList, "groupId");

        Map<String, List<KiteAttr>> newGrpAttrMap = new HashMap<>();
        Iterator<String> it = grpAttrMap.keySet().iterator();
        while (it.hasNext()) {
            String groupIdd = it.next();
            String groupIddd = DcPublicCache.getPCode(LocalDcPublicConsts.ATTR_CODE, groupIdd);
            if (!KiteStringUtils.isEmpty(groupIddd)) {
                newGrpAttrMap.put(groupIddd, grpAttrMap.get(groupIdd));
            } else {
                newGrpAttrMap.put(groupIdd, grpAttrMap.get(groupIdd));
            }
        }

        Map<String, KiteAttr> map = new HashMap<>();
        List<KiteAttr> list = newGrpAttrMap.get(groupId);
        if (!KiteListUtils.isEmpty(list)) {
            list.forEach(ka -> {
                map.put(ka.getFieldName(), ka);
            });
        }
        return map;
    }

    private void addTariffAttrInfo(List<Map<String, String>> tariffAttrList, KiteAttr kiteAttr) {
        if (Objects.nonNull(kiteAttr)) {
            if (!KiteStringUtils.equalsAny(kiteAttr.getShowType(), KeyConsts.PROD_ATTR_SHOW_TYPE_E, KeyConsts.PROD_ATTR_SHOW_TYPE_H)) {
                Map<String, String> tariffAttr = new HashMap<>();
                tariffAttr.put("fieldName", kiteAttr.getFieldName());
                tariffAttr.put("cname", kiteAttr.getCname());

                String value = kiteAttr.getValueDesc();
                if (KiteStringUtils.isEmpty(value)) {
                    value = transAttrValueDesc(kiteAttr.getAttrValues(), kiteAttr.getValue());
                }
                tariffAttr.put("value", value);
                tariffAttrList.add(tariffAttr);
            }
        }
    }
}
